package gui;

import java.util.GregorianCalendar;

import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.events.SelectionListener;
import org.eclipse.swt.layout.FormAttachment;
import org.eclipse.swt.layout.FormData;
import org.eclipse.swt.layout.FormLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.DateTime;
import org.eclipse.swt.widgets.Event;


/**
 * @author sebastian
 *
 */
public class DateTimeComposite extends Composite {

	/**
	 * Field for the time.
	 */
	private DateTime time;
	/**
	 * Field for day, month, year.
	 */
	private DateTime date;
	
	/**
	 * @param parent parent control
	 * @param style SWT-Style
	 */
	public DateTimeComposite(final Composite parent, final int style) {
		super(parent, style);
		FormLayout layout = new FormLayout();
		this.setLayout(layout);
		
		this.time = new DateTime(this, SWT.TIME);
		this.date = new DateTime(this, SWT.DROP_DOWN);
		
		FormData data = new FormData();
		data.left = new FormAttachment(0, 5);
		data.top = new FormAttachment(0, 0);
		this.date.setLayoutData(data);
		
		data = new FormData();
		data.left = new FormAttachment(this.date, 2);
		data.right = new FormAttachment(100, 0);
		data.top = new FormAttachment(0, 0);
		this.time.setLayoutData(data);
		this.date.addSelectionListener(new SelectionListener() {
			
			@Override
			public void widgetSelected(final SelectionEvent arg0) {
				notifyListeners(SWT.Modify, new Event());
			}
			
			@Override
			public void widgetDefaultSelected(final SelectionEvent arg0) {
			}
		});
		
		this.time.addSelectionListener(new SelectionListener() {
			
			@Override
			public void widgetSelected(final SelectionEvent arg0) {
				notifyListeners(SWT.Modify, new Event());
			}
			
			@Override
			public void widgetDefaultSelected(final SelectionEvent arg0) {
			}
		});
	}
	
	/* (non-Javadoc)
	 * @see org.eclipse.swt.widgets.Control#setEnabled(boolean)
	 */
	@Override
	public void setEnabled(final boolean b) {
		this.date.setEnabled(b);
		this.time.setEnabled(b);
	}
	
	/**
	 * @param b timefield enabled or disabled (true/false)
	 */
	public void setTimeEnabled(final boolean b) {
		this.time.setEnabled(b);
	}
	
	/**
	 * @return a gregoriancalendar with the values of the controls
	 */
	public GregorianCalendar getCalendar() {
		GregorianCalendar result = new GregorianCalendar();
		result.set(GregorianCalendar.YEAR, this.date.getYear());
		result.set(GregorianCalendar.MONTH, this.date.getMonth());
		result.set(GregorianCalendar.DAY_OF_MONTH, this.date.getDay());
		
		result.set(GregorianCalendar.HOUR_OF_DAY, this.time.getHours());
		result.set(GregorianCalendar.MINUTE, this.time.getMinutes());
		result.set(GregorianCalendar.SECOND, this.time.getSeconds());
		
		return result;
	}

	/**
	 * @param calendar input calendar, controls will be set to its' values.
	 */
	public void setCalendar(final GregorianCalendar calendar) {
		this.date.setYear(calendar.get(GregorianCalendar.YEAR));
		this.date.setMonth(calendar.get(GregorianCalendar.MONTH));
		this.date.setDay(calendar.get(GregorianCalendar.DAY_OF_MONTH));
		
		this.time.setHours(calendar.get(GregorianCalendar.HOUR_OF_DAY));
		this.time.setMinutes(calendar.get(GregorianCalendar.MINUTE));
		this.time.setSeconds(calendar.get(GregorianCalendar.SECOND));
	}
	
	/**
	 * @param calendar calendar to compare
	 * @return is this dateTime later than calendar?
	 */
	public boolean verify(final GregorianCalendar calendar) {
		GregorianCalendar cal1 = this.getCalendar();
		boolean isLater = (cal1.get(GregorianCalendar.YEAR) >= calendar.get(GregorianCalendar.YEAR))
				&& (cal1.get(GregorianCalendar.YEAR) >= calendar.get(GregorianCalendar.YEAR))
				&& (cal1.get(GregorianCalendar.MONTH) >= calendar.get(GregorianCalendar.MONTH))
				&& (cal1.get(GregorianCalendar.DAY_OF_MONTH) >= calendar.get(GregorianCalendar.DAY_OF_MONTH))
				&& (cal1.get(GregorianCalendar.HOUR_OF_DAY) >= calendar.get(GregorianCalendar.HOUR_OF_DAY))
				&& (cal1.get(GregorianCalendar.MINUTE) >= calendar.get(GregorianCalendar.MINUTE))
				&& (cal1.get(GregorianCalendar.SECOND) >= calendar.get(GregorianCalendar.SECOND));
		return isLater;
	}
}
